package com.yiqi.zhiyuan.feature.home.view.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import com.suozhang.framework.utils.DensityUtil;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.common.utils.AppUtil;
import com.yiqi.zhiyuan.common.utils.OrderUtil;
import com.yiqi.zhiyuan.entity.enums.OrderFlagType;
import com.yiqi.zhiyuan.entity.enums.OrderType;
import com.yiqi.zhiyuan.entity.vo.OrderVO;
import com.yiqi.zhiyuan.feature.order.view.OrderInfoActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 立即前往弹框
 *
 * @author LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/25 15:22
 */
public class ImmediatelyDialog extends Dialog implements RouteSearch.OnRouteSearchListener {

    @BindView(R.id.iv_immediately_state)
    ImageView mIvImmediatelyState;
    @BindView(R.id.tv_immediately_tips)
    TextView mTvImmediatelyTips;
    @BindView(R.id.tv_immediately_distance)
    TextView mTvImmediatelyDistance;
    @BindView(R.id.tv_immediately_type)
    TextView mTvImmediatelyType;
    @BindView(R.id.tv_immediately_departure)
    TextView mTvImmediatelyDeparture;
    @BindView(R.id.tv_immediately_destination)
    TextView mTvImmediatelyDestination;
    @BindView(R.id.tv_order_point_1)
    TextView mTvOrderPoint1;
    @BindView(R.id.tv_order_point_2)
    TextView mTvOrderPoint2;
    private Context context;

    private MyClickListenerInterface listenerInterface;
    private LayoutInflater layoutInflater;
    OrderVO orderVO;

    private RouteSearch routeSearch;
    private RouteSearch.FromAndTo fromAndTo;
    private AMapLocation amapLocation;

    public ImmediatelyDialog(Context context, OrderVO orderVO,AMapLocation amapLocation) {
        super(context, R.style.ActionImmediatelyDialogStyle);
        this.context = context;
        this.orderVO = orderVO;
        this.amapLocation = amapLocation;
        layoutInflater = LayoutInflater.from(context);
        routeSearch = new RouteSearch(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();

        if (amapLocation != null) {
            LatLonPoint mStartPoint = new LatLonPoint(amapLocation.getLatitude(), amapLocation.getLongitude());
            LatLonPoint mEndPoint = new LatLonPoint(orderVO.getDepLatitude(), orderVO.getDepLongitude());
            fromAndTo = new RouteSearch.FromAndTo(
                    mStartPoint, mEndPoint);
            routeSearch.setRouteSearchListener(this);
            // fromAndTo包含路径规划的起点和终点，drivingMode表示驾车模式
            // 第三个参数表示途经点（最多支持16个），第四个参数表示避让区域（最多支持32个），第五个参数表示避让道路
            RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.DrivingDefault, null, null, "");
            routeSearch.calculateDriveRouteAsyn(query);
        }
    }

    private void init() {
        View view = layoutInflater.inflate(R.layout.dialog_order_immediately, null);
        ButterKnife.bind(this, view);
        setContentView(view);
        mTvImmediatelyDeparture.setText(orderVO.getDeparture());
        mTvImmediatelyDestination.setText(orderVO.getDestination());

        mTvImmediatelyType.setText(OrderUtil.getOrderTypeString(orderVO));
        mTvOrderPoint1.setVisibility(TextUtils.isEmpty(orderVO.getWaypoint1())?View.GONE:View.VISIBLE);
        mTvOrderPoint2.setVisibility(TextUtils.isEmpty(orderVO.getWaypoint2())?View.GONE:View.VISIBLE);
        mTvOrderPoint1.setText(TextUtils.isEmpty(orderVO.getWaypoint1())?"":"途径点："+orderVO.getWaypoint1());
        mTvOrderPoint2.setText(TextUtils.isEmpty(orderVO.getWaypoint2())?"":"途径点："+orderVO.getWaypoint2());

        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        getWindow().getDecorView().setPadding(DensityUtil.dp2px(43), 0, DensityUtil.dp2px(43), 0);
        getWindow().setAttributes(layoutParams);

    }

    public void setListener(MyClickListenerInterface listener) {
        this.listenerInterface = listener;
    }

    @OnClick({R.id.btn_immediately_go, R.id.btn_immediately_exit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_immediately_go:
                dismiss();
                if (listenerInterface != null) {
                    listenerInterface.doConfirm(orderVO);
                }
                break;
            case R.id.btn_immediately_exit:
                dismiss();
                Intent intent = new Intent(context, OrderInfoActivity.class);
                intent.putExtra("order_info", orderVO);
                context.startActivity(intent);
                break;
        }
    }

    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                DrivePath walkPath = result.getPaths().get(0);
                mTvImmediatelyDistance.setText(String.format("距您%s公里",AppUtil.formatFloat(walkPath.getDistance() / 1000)));
            } else {
//                T.showShort(R.string.toast_no_result);
            }
        } else {

        }
    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

    }

    @Override
    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

    }


    public interface MyClickListenerInterface {
        void doConfirm(OrderVO orderVO);
    }
}
